package com.wbdgj.ui.branch;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.adapter.CompaignAdapter;
import com.wbdgj.adapter.CompaignMoreAdapter;
import com.wbdgj.adapter.MessageAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultMessageObjectModel;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.home.CampaignDetailActivity;
import com.wbdgj.ui.near.MessageBoardDetailActivity;
import com.wbdgj.ui.word_compaign.WordCompaignActivity;
import com.wbdgj.utils.ListViewForScrollView;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;

public class MoreCampaignActivity extends BaseActivity {

    private Context context = this;
    private String id;

    @BindView(R.id.demo_listview)
    PullListView demo_listview;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;


    private CompaignMoreAdapter compaignAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_more_campaign;
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra("id");
        appbranchactivity();
        demo_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!compaignAdapter.hasNoData){
                    startActivity(new Intent(context, WordCompaignActivity.class).putExtra("id", compaignAdapter.getItem(i).get("cloud_id")+""));
                }
            }
        });

        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        appbranchactivity();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });

    }

    private void appbranchactivity() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appnbnactivity(id, "2");
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    ArrayList<LinkedTreeMap> arrayList = (ArrayList<LinkedTreeMap>) linkedTreeMap.get("data");
                    compaignAdapter = new CompaignMoreAdapter(context, arrayList);
                    demo_listview.setAdapter(compaignAdapter);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
